package com.label.Service.Impl;/**
 * @author zhf
 * @date 2023/3/22 18:55
 * @version 1.0
 */

import com.label.Dao.VerificationMapper;
import com.label.Model.Query.UserQuery;
import com.label.Service.VerificationService;
import com.label.Utils.Common.AesCipherUtil;
import com.label.Utils.Common.SendEmailUtil;
import com.label.Utils.Common.ValidateCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

/**
 * @author zhf
 * 项目：studentLabel
 * 描述：
 * @date 2023/3/22 18:55
 **/
@Service
@Slf4j
@Transactional(rollbackFor = {Exception.class})
public class VerificationServiceImpl implements VerificationService {


    @Autowired
    private VerificationMapper verificationMapper;
    @Autowired
    private SendEmailUtil sendEmailUtil;


    @Override
    public String sendVerification(String email) {
        //1 email是否存在在我们的 复试人员信息中，不存在
        Integer verificationCode;
        if(verificationMapper.exitEmail(email)){
            //1.1 邮箱存在 发送验证码
            verificationCode = ValidateCodeUtil.generateValidateCode(6);
            log.info("验证码为：{}", verificationCode);
            sendEmailUtil.sendAuthCodeEmail(email,verificationCode.toString());
        }else {
            throw new RuntimeException("邮箱不存在");
        }
        return String.valueOf(verificationCode);
    }

    @Override
    public UserQuery getLoginInfo(String email) {
        //1 根据邮箱获得账号密码
        HashMap<String,String> info = (HashMap<String, String>) verificationMapper.selAccountPwdByEmail(email);
        //2 解析加密的密码
        UserQuery userQuery = new UserQuery(info.get("ACCOUNT"));
        String accoutPwd = AesCipherUtil.deCrypto(info.get("PASSWORD"));
        String pwd = accoutPwd.replaceFirst(info.get("ACCOUNT"),"" );

        //3 填充到userQuery实体中
        userQuery.setPassword(pwd);

        return userQuery;
    }
}
